%% Use of \sprintf{...} in conditional formats
% by Jaromir Benes
% 
% In this m-file, we use the `\sprintf{...}` command in conditional format
% structs to modify the way selected numeric entries are printed in tables
% or matrices.

%% Clear Workspace

clear;
close all;
home
irisrequired 20120827;

%% Create artificial data
%
% We create two random time series, <?tseries?>, to be reported in a
% `table` command, and a random matrix, <?matrix?>, to be reported in a
% `matrix` command.

x = 10*tseries(yy(2010):yy(2016),@rand); %?tseries?
y = 10*tseries(yy(2010):yy(2016),@rand);

M = rand(5) - 0.5; %?matrix?

%% Start a new report

r = report.new();

%% Table with conditional numeric format
%
% We report a two-row table with entries in years before 2013 printed with
% no decimal places and in red colour; all other entries are printed using
% the default format, `'%.2f'`, i.e. to two decimal places. See also help
% on the standard Matlab function `sprintf` for details on how to write the
% numeric format strings.
%
% Note that `'\sprintf{...}'` is an IRIS extension, not a genuinte LaTeX
% command, and is removed from the final LaTeX code before compilation. You
% can see this when you use the option `'saveas='` to save the LaTeX code
% created for this table.

c1 = struct();
c1.test = 'date < yy(2013)';
c1.format = '\sprintf{%.0f} \color{red}';

r.table('Table with conditional numeric format', ...
    'range=',yy(2010):yy(2016), ...
    'condformat=',c1, ...
    'saveas=','sprintf_in_condformat_table.tex'); 

r.series('First series',x);
r.series('Second series',y);

disp('LaTeX code for the table');
type sprintf_in_condformat_table.tex;

%% Matrix with conditional numeric format
%
% We report a 5-by-5 table with all entries greater than 0.2 in absolute
% value printed to ten decimal places and always with a sign (`+` or `-`).
% All other numbers are printed (by default) to two decimal places.

c2 = struct();
c2.test = 'abs(value) > 0.25';
c2.format = '\sprintf{%+.10f}';

r.matrix('Matrix with conditional numeric format',M,'condformat=',c2);

%% Publish report to PDF
%
% Compile the report into a PDF named `'sprintf_in_condformat.pdf'`. The
% `open` command to open the PDF <?open?> may not work on some systems.

r.publish('report_sprintf_in_condformat.pdf');

open report_sprintf_in_condformat.pdf; %?open?

%% Help on IRIS functions used in this m-file
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in an HTML browser window.
%
%    help report
%    help report/new
%    help report/table
%    help report/series
%    help report/matrix
%    help report/publish
%    help tseries
%    help tseries/tseries
